//
//  SearchListTableViewController.m
//  BlueToothDemo
//
//  Created by US bike on 2017/8/24.
//  Copyright © 2017年 US bike. All rights reserved.
//

#import "SearchListTableViewController.h"
#import <CoreBluetooth/CoreBluetooth.h>


@interface PeripheralInfo : NSObject

@property (nonatomic, copy) NSString *name;
@property (nonatomic, copy) NSString *uuid;

@end

@implementation PeripheralInfo



@end

@interface SearchListTableViewController ()<CBCentralManagerDelegate>

@property (nonatomic, strong) NSMutableArray<PeripheralInfo *> *peripheralMutableArray;
@property (nonatomic, strong) CBCentralManager *centralManager;


@end

@implementation SearchListTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.centralManager = [[CBCentralManager alloc] initWithDelegate:self queue:dispatch_get_main_queue()];
    
}

#pragma mark - CBCentralManagerDelegate

- (void)centralManagerDidUpdateState:(CBCentralManager *)central {
    NSArray *arrray;
    switch (central.state) {
        case CBManagerStatePoweredOn:
            
            NSLog(@"连接成功了，要怎么样");
            
            [central scanForPeripheralsWithServices:nil options:nil];

            break;
            
        default:
            break;
    }
}


- (void)centralManager:(CBCentralManager *)central willRestoreState:(NSDictionary<NSString *, id> *)dict {
    //NSLog(@"willRestoreState方法发出的消息：%@", dict);
}


- (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary<NSString *, id> *)advertisementData RSSI:(NSNumber *)RSSI {
    
    NSLog(@"找到蓝牙设备，名字：%@, UUID:%@ ", peripheral.name, peripheral.identifier.UUIDString);
    PeripheralInfo *info = [PeripheralInfo new];
    info.name = peripheral.name;
    info.uuid = peripheral.identifier.UUIDString;
    if (!info.name) {
        info.name = @"unknow";
    }
    
    
    [self.peripheralMutableArray addObject:info];
    [self.tableView reloadData];
    
}

- (void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral {
    
}


- (void)centralManager:(CBCentralManager *)central didFailToConnectPeripheral:(CBPeripheral *)peripheral error:(nullable NSError *)error {
    
}


- (void)centralManager:(CBCentralManager *)central didDisconnectPeripheral:(CBPeripheral *)peripheral error:(nullable NSError *)error {
    
}

#pragma mark - Table view data source

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.peripheralMutableArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"searchListCell" forIndexPath:indexPath];
    
    PeripheralInfo *info = self.peripheralMutableArray[indexPath.row];
    cell.textLabel.text = info.name;
    cell.detailTextLabel.text = info.uuid;
    return cell;
}


#pragma mark - getter setter
- (NSMutableArray<PeripheralInfo *> *) peripheralMutableArray {
    if (!_peripheralMutableArray) {
        _peripheralMutableArray = [NSMutableArray array];
    }
    return _peripheralMutableArray;
}


-(void) dealloc {
    NSLog(@"销毁界面");
    
    [self.centralManager stopScan];
}

@end
